package com.abbas.CayTcore.model

import com.abbas.CayTcore.base.BaseModel
import com.abbas.CayTcore.entity.TCoreBase
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.CompletableFuture
import java.util.concurrent.Future

class CoreDetailModel : BaseModel() {

    fun updateCore(core: TCoreBase,success: () -> Unit,failure: (e: Throwable) -> Unit) {
//        CompletableFuture.runAsync {
//            var updateCore = db.coreDao()
//                .updateCore(core)
//        }
        Observable.create<Int> {
            it.onNext(
                db.coreDao()
                    .updateCore(core)
            )
        }.observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe ({
                        if (it == 1){
                            success.invoke()
                        }else{
                            failure.invoke(Throwable("Update Core Failure"))
                        }
            },{
              failure.invoke(it)
            },{
                success.invoke()
            })
    }


    fun deleteCore(core: TCoreBase,success: () -> Unit,failure: (e: Throwable) -> Unit){
        Observable.create<Int> {
            it.onNext(
                db.coreDao()
                    .deleteCore(core)
            )
        }.observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe ({

            },{
                failure.invoke(it)
            },{
                success.invoke()
            })
    }
}